<template>
  <div class="wrapper">
    <div class="bottom">
      <div>
        <MsgItem title="总库存" :value="stock + '件'"></MsgItem>
      </div>
      <div>
        <MsgItem title="余额" :value="'￥' + balance" :highLight="true"></MsgItem>
      </div>
      <router-link tag="div" to="/center/merchantCenter/merchantOrderList">
        <MsgItem title="被指派订单" value="查看"></MsgItem>
      </router-link>
      <router-link tag="div" to="/center/merchantCenter/merchantReturnList">
        <MsgItem title="退单列表" value="查看"></MsgItem>
      </router-link>
      <!-- <div @click="drawcash">
        <MsgItem title="提现" value="查看"></MsgItem>
      </div> -->
      <router-link tag="div" :to="'/center/bankList'">
        <MsgItem title="银行卡列表" value="查看"></MsgItem>
      </router-link>
    </div>
  </div>
</template>

<script>
  import MsgItem from '@/components/msgItem/MsgItem.vue'
  import { mapState } from 'vuex'
  import { addInt, removeInt } from '@/assets/js/axios.js'
  export default {
    name: 'MerchantCenter',
    components: {
      MsgItem
    },
    created() {
      removeInt()
      this._getBalance()
      this._getStock()
      this._getBankCount()
    },
    data() {
      return {
        balance: '',
        stock: '',
        bankCount: ''
      }
    },
    computed: {
      ...mapState(['openId'])
    },
    methods: {
      async _getBalance() {
        try{
          let { data: { code, data } } = await this.$axios({
            url: '/v1/platform/balance',
            method: 'GET',
            params: {
              openid: this.openId
            }
          })
          if (code === 0) {
            this.balance = data
          } else if(code === 1) {
            this.balance = 0
          }
          addInt()
          this.$toast.clear()
        }catch(e){
          this.$toast(e)
        }
      },
      async _getStock() {
        try{
          let { data: { code, data } } = await this.$axios({
            url: '/v1/platform/stock',
            method: 'GET',
            params: {
              openid: this.openId
            }
          })
          if (code === 0) {
            this.stock = data
          }
          if(code === 1) {
            this.stock = 0
          }
          addInt()
        }catch(e){
          this.$toast(e)
        }
      },
      async _getBankCount() {
        let { data: { code, data } } = await this.$axios({
          url: '/v1/bank/show',
          method: 'GET',
          params: {
            openid: this.openId
          }
        })
        if (code === 0) {
          this.bankCount = data.items.length
        }
      },
      drawcash() {
        if(this.bankCount) {
          this.$router.push('/center/drawcash/' + this.balance)
        } else {
          this.$toast('请先添加银行卡')
          setTimeout(() => {
            this.$router.push('/center/bankAdd/none')
          }, 1500)
        }
      }
    }
  }
</script>

<style lang="scss" scoped>
  @import '@/assets/css/base.scss';
  .wrapper {
    padding: 0.2rem 0;
    font-size: 0.28rem;
    background: #eee;
    height: 100vh;
  }
  .bottom {
    margin-top: 0.2rem;
    padding: 0 0.1rem;
    background: #fff;
  }
</style>
